//
//  ViewController.m
//  IOS6
//
//  Created by blue on 15-11-30.
//  Copyright (c) 2015年 zx. All rights reserved.
//

#import "ViewController.h"
#import "input2ViewController.h"
@interface ViewController () <ZXDelegate>
{
    NSMutableArray * name;
    NSMutableArray * tel;
    NSIndexPath * i;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.title = @"电话通讯录";
    name = @[
             @"张鑫",
             @"肖遥"
             ].mutableCopy;
    tel = @[
            @"15073215896",
            @"13762214622"
            ].mutableCopy;

    //self.navigationItem.rightBarButtonItem = self.editButtonItem;
     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(Editing :)];
    self.tableView.allowsSelectionDuringEditing =YES;
    
}

- (void)Editing:(UIBarButtonItem * )ed
{
    [self.tableView setEditing:!self.tableView.editing animated:YES];
    ed.title = self.editing ? @"完成" : @"编辑";
    [self.tableView reloadData];
   
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return tableView.isEditing ? name.count + 1 : name.count;;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return indexPath.row - name.count ? UITableViewCellEditingStyleDelete:UITableViewCellEditingStyleInsert;

}

-(UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
//    显示行
    if (indexPath.row < name.count) {
       cell.textLabel.text = name[indexPath.row];
        cell.detailTextLabel.text = tel[indexPath.row];
    }
 
       cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    //[tableView deselectRowAtIndexPath:indexPath animated:YES];
    input2ViewController * con = [[input2ViewController alloc]initWithStyle:UITableViewStyleGrouped];
    if (indexPath.row < name.count) {
        con.tfname1 = name[indexPath.row];
        con.tftel1 = tel[indexPath.row];
    }
    else
    {
        [name addObject:@""];
        [tel addObject:@""];
    }
    i = indexPath;
    con.delegate = self;
    [self.navigationController pushViewController:con animated:YES];
    if (tableView.editing) {
        [tableView setEditing:NO animated:YES];
    }
}

- (void)InputFishWithname:(NSString *)names tel:(NSString *)tels
{
    NSMutableArray * arr = name.mutableCopy;
    name = arr;
    NSIndexPath * indexPath  = i;
    name[indexPath.row]=names;
    tel[indexPath.row]=tels;
    [self.tableView reloadData];
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleInsert) {
        
    }else {
        [name removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }
}






- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
